package com.ljx.rabbit.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ljx.rabbit.context.BaseContext;
import com.ljx.rabbit.domain.po.Address;
import com.ljx.rabbit.domain.po.Area;
import com.ljx.rabbit.domain.po.Districts;
import com.ljx.rabbit.mapper.AddressMapper;
import com.ljx.rabbit.service.IAddressService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ljx.rabbit.util.HttpClientUtil;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author author
 * @since 2025-02-05
 */
@Service
public class AddressServiceImpl extends ServiceImpl<AddressMapper, Address> implements IAddressService {
    @Value("${rabbit.GaoDe.key}")
    private String key;

    private static final String URL = "https://restapi.amap.com/v3/config/district";

    @Override
    public List<Districts> getArea(String keywords) {
        Map<String, String> parameters = new HashMap();
        parameters.put("key", key);
        if (keywords != null) {
            //查询省级以下地区
            parameters.put("keywords", keywords);
        }
        parameters.put("subdistrict", "2");
        String s = HttpClientUtil.doGet(URL, parameters);
        Area area = JSON.parseObject(s, Area.class);
        List<Districts> districts1 = area.getDistricts();
        Districts districts2 = districts1.get(0);
        List<Districts> districts = districts2.getDistricts();//若查到最低层，返回的是[]
        System.out.println(districts);
        return districts;
    }

    @Override
    public List<Address>  listAddress() {
        QueryWrapper<Address> wrapper = new QueryWrapper<>();
        wrapper.lambda().eq(Address::getUserId, BaseContext.getCurrentId());
        return list(wrapper);
    }
}
